package com.gzdh.dishrecognition.ui.adapter.defaultview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.ProductConst;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.event.CartEvent;
import com.gzdh.dishrecognition.model.ProductCartModel;
import com.gzdh.dishrecognition.model.WeekMenuBakModel;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class DefaultCartAdapter extends RecyclerView.Adapter<DefaultCartAdapter.MyViewHolder> {


    private LayoutInflater inflater;

    private Context mContext;

    private FoodsAdapter.OnClick onClick;

    public void setOnClick(FoodsAdapter.OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void userOnClick(WeekMenuBakModel.Product product);
    }

//    public void setDatas(List<WeekMenuBakModel.Product> datas) {
//        this.datas = datas;
//        notifyDataSetChanged();
//    }

    public DefaultCartAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }


    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_default_cart, parent, false);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ProductCartModel productCartModel = ProductConst.getInstance().productCartModelList.get(position);
        holder.nameTv.setText(productCartModel.getName());
        holder.nameTv.setTextSize(SysConstConfig.getInstance().getShopCartFontSize());
        holder.countTv.setText(String.valueOf(productCartModel.getNumber()));
        holder.countTv.setTextSize(SysConstConfig.getInstance().getShopCartFontSize());

        BigDecimal amountPrice = new BigDecimal(productCartModel.getPrice()).multiply(new BigDecimal(productCartModel.getNumber()));
        holder.amountTv.setText(amountPrice.setScale(2, RoundingMode.DOWN).toPlainString());
        holder.amountTv.setTextSize(SysConstConfig.getInstance().getShopCartFontSize());
//        holder.mainLinear.setOnClickListener(view -> {
//            List<ProductCartModel> productCartModelList = new ArrayList<>();
//            for (int i = 0; i < ProductConst.getInstance().productCartModelList.size(); i++) {
//                ProductCartModel productCartModel2 = ProductConst.getInstance().productCartModelList.get(i);
//                if (position != i) {
//                    productCartModelList.add(productCartModel2);
//                }
//            }
//            ProductConst.getInstance().setProductCartModelList(productCartModelList);
//            EventBus.getDefault().post(new CartEvent());
//        });
        holder.addBut.setVisibility(View.GONE);
        holder.addBut.setOnClickListener(view -> {
            ProductConst.getInstance().getProductCartModelList().get(position).setNumber(
                    ProductConst.getInstance().productCartModelList.get(position).getNumber() + 1
            );
            EventBus.getDefault().post(new CartEvent());
        });

        holder.subBut.setVisibility(View.GONE);
        holder.mainLinear.setOnClickListener(view -> {
            ProductConst.getInstance().getProductCartModelList().get(position).setNumber(
                    ProductConst.getInstance().productCartModelList.get(position).getNumber() - 1
            );

            List<ProductCartModel> productCartModelList = new ArrayList<>();
            for (int i = 0; i < ProductConst.getInstance().productCartModelList.size(); i++) {
                ProductCartModel productCartModel2 = ProductConst.getInstance().productCartModelList.get(i);
                if (productCartModel2.getNumber() > 0) {
                    productCartModelList.add(productCartModel2);
                }
            }
            ProductConst.getInstance().setProductCartModelList(productCartModelList);

            EventBus.getDefault().post(new CartEvent());
        });
    }


    //数据源的数量
    @Override
    public int getItemCount() {
        return ProductConst.getInstance().productCartModelList.size();
    }

    public void subFinalLine() {
        if(ProductConst.getInstance().productCartModelList.size() == 0) {
            return;
        }
        int position = ProductConst.getInstance().productCartModelList.size() - 1;
        ProductConst.getInstance().getProductCartModelList().get(position).setNumber(
                ProductConst.getInstance().productCartModelList.get(position).getNumber() - 1
        );
        List<ProductCartModel> productCartModelList = new ArrayList<>();
        for (int i = 0; i < ProductConst.getInstance().productCartModelList.size(); i++) {
            ProductCartModel productCartModel2 = ProductConst.getInstance().productCartModelList.get(i);
            if (productCartModel2.getNumber() > 0) {
                productCartModelList.add(productCartModel2);
            }
        }
        ProductConst.getInstance().setProductCartModelList(productCartModelList);
        EventBus.getDefault().post(new CartEvent());
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout mainLinear;
        private TextView nameTv;
        private TextView countTv;
        private TextView amountTv;

        private TextView addBut;

        private TextView subBut;


        public MyViewHolder(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.nameTv);
            countTv = (TextView) itemView.findViewById(R.id.countTv);
            amountTv = (TextView) itemView.findViewById(R.id.amountTv);

            mainLinear = (LinearLayout) itemView.findViewById(R.id.mainLinear);


            addBut = (TextView) itemView.findViewById(R.id.addBut);
            subBut = (TextView) itemView.findViewById(R.id.subBut);

        }
    }
}

